<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">用户管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>用户管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
         <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">手机号</label>
          <div class="layui-input-block">
            <input type="text" name="phone" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <button  class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
          <button id="btn-reset" class="layui-btn icon-btn"><i class="layui-icon">&#x1006;</i>重置</button>
          <% if(add) { %> <button id="user-btn-add" class="layui-btn icon-btn" ><i class="layui-icon">&#xe654;</i>添加用户
            </button> <% } %>
        </div>
      </div>
           
        </div> 

        <table class="layui-table" id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="user-table-bar">
  
    <% if(edit) { %> <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a> <% } %>
     <% if(updatePsw) { %> <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a> <% } %>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <% if(delete) { %><input type="checkbox" lay-filter="user-tpl-state" value="{{d.userId}}" lay-skin="switch" lay-text="正常|停用"
           {{d.state==0?'checked':''}}/> <% } %>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'element','treetable'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var treetable = layui.treetable;
        var element = layui.element;

        form.render('select');

/*         var renderTable = function () {
        // 渲染表格
        //treetable.render({ */
        table.render({
        	
            page: true,
            
            elem: '#user-table',
            url: 'system/user/list',
            where: {shopId: $("#shopInfo").attr("data")},
            cols: [[
                {type: 'numbers'},
                {field: 'username', title: '帐号'},
                {field: 'nickName', title: '用户名'},
                {field: 'phone',  title: '手机号'},
                {field: 'roleName', title: '角色'},
                {field:'createTime', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {field: 'state',  templet: '#user-tpl-state', title: '状态'},
                {align: 'center', toolbar: '#user-table-bar', title: '操作'}
            ]]
        });
/*         }
        
        renderTable(); */

        // 添加按钮点击事件
        $('#user-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(user-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    $.post('system/user/restPsw', {
                        userId: obj.data.userId
                    }, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    });
                });
            }
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = data ? '修改用户' : '添加用户';
            admin.putTempData('t_user', data);
            var str =  data ? JSON.stringify(data) : " ";
            var edit = data ? "1" : "0"
            admin.popupCenter({
                title: title,
                path: 'system/user/editForm?shopId='+$("#shopInfo").attr("data")+'&edit='+edit,
                data:JSON.stringify(data),
                finish: function () {
                	location.reload();
                }
            });
            
        };

        
        //监听搜索
        form.on('submit(LAY-front-search)', function(data){
          var field = data.field;
          table.reload('user-table', {where: {username: field.username,phone:field.phone}});
        });
        

        // 修改user状态
        form.on('switch(user-tpl-state)', function (obj) {
            layer.load(2);
            $.post('system/user/updateState', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            });
        });
        
        // 搜索按钮点击事件
		$('#btn-reset').click(function() {
			location.reload();
		});
    });
</script>